package View;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Model.Player;
import Model.IUpdatable;
import Model.Planet.Planet;
import Run.Engine;

public class Universe extends JPanel implements IUpdatable {
	
	/**
	 * 
	 */
	private static Universe panel;

	/**
	 * 
	 */
	public static MouseAdapter adapter;
	
	/** Creates the one instance of the Universe
	 * @return the Universe panel
	 */
	public static Universe getInstance(){
		if(panel == null){
			panel = new Universe();
			panel.setPreferredSize(new Dimension(475, 360));
			panel.addMouseListener(new MouseAdapter(){
			    public void mouseClicked(MouseEvent e){
			        for(Planet planet: Run.Engine.planets.values()){
			            if(planet.getBounds().contains(e.getX(), e.getY())){
			            	Planet playerPlanet = Player.getInstance().getCurrentPlanet();
			            	planet.setFuelNeeded(Engine.distance(playerPlanet.getCoord().x, playerPlanet.getCoord().y, planet) / 3.14);
			            	Object stringArray[] = { "Travel" };
			            	int choice = JOptionPane.showOptionDialog(panel, planet, "Select an Option",
			            	        JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE, 
			            	            null, stringArray,
			            	        stringArray[0]);
			            	if(choice == JOptionPane.OK_OPTION){
			            		double newFuel = Player.getInstance()
			            		        .getShip().getFuel() - planet.getFuelNeeded();
			            		if(newFuel >= 0){
			            			if(Player.getInstance().getCurrentPlanet()
			            			        .compareTo(planet) != 0){
				            			Player.getInstance().setCurrentPlanet(planet);
				            			Player.getInstance().getShip().setFuel(newFuel);
				            			GUI.getInstance().setVisible(false);
				            			SpaceTravelGUI.getInstance().setVisible(true);
				            			SpaceTravelGUI.getInstance().setContentPane
				            			    (new SpaceTravelView(),  410, 290);
				            			
				            			Universe.getInstance().repaint();
			            			}
			            			else{
			            				JOptionPane.showMessageDialog
			            				    (panel, "You are currently on this planet.");
					            		
			            			}
			            		
			            		}
			            		else{
			            			JOptionPane.showMessageDialog(panel, "Sorry. You cur" +
			            					"rently do not have enough fuel to " +
			            					"travel here.");
			            		}
			            	}
			            }
			        }
			    }
			});

		}
		
		return panel;
	}
	
	/* (non-Javadoc)
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	@Override
	protected void paintComponent(Graphics g){
		super.paintComponent(g);
		BufferedImage image = null;
		try{
			image = ImageIO.read(new File("src\\View\\space.jpg"));
			
		}
		catch(IOException e){
			System.out.println(e.getMessage());
			}
			
		g.drawImage(image, 0, 0, null);
		for(Planet planet: Engine.planets.values()){
			planet.draw(g);
		}
		
		Player.getInstance().getCurrentPlanet().setCurrentPlanet(g);
	}
	
	/** Adds panel to the Universe panel
	 * @param panel the panel to add
	 */
	public void setPanel(Universe panel){
		this.panel.removeAll();
		this.panel.add(panel);
	}

	@Override
	public void update(){
	    System.out.println("Blank");
		
	}
	
	
	    
	    
	
	
	
	
}
